﻿using System;
using System.Collections.Generic;
using System.Linq;
// using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

using System.IO;
using System.IO.IsolatedStorage;
using System.Collections.ObjectModel;
using System.Diagnostics;

namespace GPA_Calculator
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        public ObservableCollection<Grade> Grades { get; set; }
        public Grade grade = null;

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            Grade.MinorInterval = App.getMinorIntervalFromStorage();
        }
        
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            // init
            grade = new Grade();
            Grades = new ObservableCollection<Grade>();

            // binding
            gradeList.ItemsSource = Grades;
            gradeChanged();
        }


        /*
         * Buttons Trigger 
         */
        private void mainGrade_Click(object sender, RoutedEventArgs e)
        {
            Button sendButton = sender as Button;
            string mainGrade = sendButton.Content.ToString();
            grade.MainGrade = mainGrade[0];
            grade.SubGrade = '0';

            gradeChanged();
        }

        private void subGrade_Click(object sender, RoutedEventArgs e)
        {
            Button sendButton = sender as Button;
            string subGrade = sendButton.Content.ToString();
            grade.SubGrade = subGrade[0];

            gradeChanged();
        }

        private void credit_Click(object sender, RoutedEventArgs e)
        {
            Button sendButton = sender as Button;
            string credit = sendButton.Content.ToString();
            grade.Credit = Int32.Parse(credit);

            gradeChanged();
        }


        private void AddButton_Click(object sender, RoutedEventArgs e)
        {
            Grades.Add(grade);
            gradeList.ScrollIntoView((object)gradeList.Items[Grades.Count - 1]);
            
            // Allocate new instance
            grade = new Grade(grade);

            // Calculate GPA
            calculateGPA();
        }

        private void calculateGPA()
        {
            GPAStatusText.Text = "Calculating...";

            var totalCredit = (from g in Grades
                               select g.Credit).Sum();

            var totalGradePoint = (from g in Grades
                                   select g.Credit * g.GradePoint).Sum();

            GPAStatusText.Text = (totalGradePoint / totalCredit).ToString("0.00");

            GPAStatusText.Text = String.Format("Total {0} Credit(s), GPA: {1}", totalCredit, (totalGradePoint / totalCredit).ToString("0.00"));
        }


        private void gradeChanged()
        {
            gradeStatus.Text = grade.ToString();
        }

        private void delButton_Click(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;
            Grade g = btn.DataContext as Grade;
            
            Grades.Remove(g);
            
            // Calculate GPA
            calculateGPA();
        } 


        /* 
         * Application Bar Triggers 
         */
        private void AppBarAbout_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/AboutPage.xaml", UriKind.Relative));
        }

        private void AppBarHowTo_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/HelpPage.xaml", UriKind.Relative));
        }

        private void AppBarSettings_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Settings.xaml", UriKind.Relative));
        }


        /*
         * Sub methods 
         */

        /*
        private float getMinorIntervalFromStorage()
        {
            IsolatedStorageSettings iss = IsolatedStorageSettings.ApplicationSettings;

            float minorInterval;
            string keyName = "MinorInterval";
            try
            {
                iss.TryGetValue(keyName, out minorInterval);
            }
            catch (Exception e)
            {
                // Set Default Value
                minorInterval = 0.5F;
                Debug.WriteLine(e.StackTrace);
            }

            if (minorInterval == 0F) minorInterval = 0.5F;

            return minorInterval;

        }
         */
    }
}